﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using facebook.Schema;
using facebook.web;

namespace FacebookApp
{
    public partial class Admins_User : Page
    {
        private static bool Insert;
        private static string pass_temp = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["folder"] = "User";
                BindGrid();
                ShowDll();
            }
        }

        private void BindGrid()
        {
            grdUser.DataSource = UserService.User_GetByAll();
            grdUser.DataBind();
        }



        protected void btnThem_Click(object sender, EventArgs e)
        {
            pnShow.Visible = false;
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            //txtFB_Id.Text = Master.API.uid.ToString();
            Session["upload"] = null;
            Insert = true;

            //FriendList1.Friends = Master.API.friends.getUserObjects();
            //IList<album> albums = Master.API.photos.getAlbums();
            //facebook.Schema.user userinfo = Master.API.users.getInfo(Master.API.uid);
            //Label1.Text = "Hi, " + userinfo.first_name + userinfo.uid + "/" + Master.API.uid;
            //Image1.ImageUrl = userinfo.pic;
            //foreach (user u in FriendList1.Friends)
            //{
            //    DateTime bdate = u.birthday_date;
            //    DropDownList1.Items.Add(u.first_name);
            //}
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdUser.Items.Count; i++)
                {
                    item = grdUser.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                        {
                            string strId = item.Cells[1].Text;
                            try
                            {
                                UserService.User_Delete(strId);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                grdUser.CurrentPageIndex = 0;
                BindGrid();
            }
            catch
            {
            }
        }

        protected void grdUser_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var strCA = new UserInfo();
            strCA.Id = e.CommandArgument.ToString();
            if (e.CommandName == "Edit")
            {
                User dt = UserService.User_GetById(strCA.Id);
                txtID.Text = dt.Id.ToString();
                txtFB_Id.Text = dt.FB_Id.ToString();
                txtUserName.Text = dt.UserName.ToString();
                txtFullName.Text = dt.FullName.ToString();
                txtAddress.Text = dt.Address.ToString();

                pass_temp = dt.Password.ToString();
                txtPassword.Text = dt.Password.ToString();

                txtPhone.Text = dt.Phone.ToString();
                txtEmail.Text = dt.Email.ToString();
                ddlStatus.SelectedValue = dt.Status.ToString();
                //txtDateLogin.Text = DateTime.Now.ToString();
                dllGroupUser_Id_Update.SelectedValue = dt.GroupUser_Id.ToString();
                
                pnUpdate.Visible = true;
                pnShow.Visible = false;
            }
            else
            {
                UserService.User_Delete(strCA.Id);
                BindGrid();
            }
        }

        protected void lblBack_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            Insert = false;
        }

        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();

            #region[TestInput]

            if (txtFB_Id.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("FB_Id not null!");
                txtFB_Id.Focus();
                return;
            }
            if (txtUserName.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("UserName not null!");
                txtUserName.Focus();
                return;
            }
            if (Insert && txtPassword.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Password not null!");
                txtPassword.Focus();
                return;
            }  
            if (txtFullName.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("FullName not null!");
                txtFullName.Focus();
                return;
            }
            if (txtAddress.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Address not null!");
                txtAddress.Focus();
                return;
            }
                      

            if (txtPhone.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Phone not null!");
                txtPhone.Focus();
                return;
            }
            if (txtEmail.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Email not null!");
                txtEmail.Focus();
                return;
            }
            //if (txtStatus.Text.Trim().Equals(""))
            //{
            //    WebMsgBox.Show("Status not null!");
            //    txtStatus.Focus();
            //    return;
            //}

            if (dllGroupUser_Id_Update.SelectedIndex == 0)
            {
                WebMsgBox.Show("Select Group User!");
                dllGroupUser_Id_Update.Focus();
                return;
            }

            #endregion

            var obj = new UserInfo();
            obj.Id = txtID.Text;
            obj.FB_Id = txtFB_Id.Text;
            obj.UserName = txtUserName.Text;
            obj.FullName = txtFullName.Text;
            obj.Address = txtAddress.Text;

            if (Insert)
            {
                obj.Password = FunctionClass.encryptData(txtPassword.Text.Trim());
            }
            else 
            {
                if (txtPassword.Text.Trim().Equals(""))
                    obj.Password = pass_temp;
                else
                    obj.Password = FunctionClass.encryptData(txtPassword.Text.Trim());
            }

            obj.Phone = txtPhone.Text;
            obj.Email = txtEmail.Text;
            obj.Status = ddlStatus.SelectedValue;
            //obj.DateLogin = txtDateLogin.Text;
            obj.GroupUser_Id = dllGroupUser_Id_Update.SelectedValue;
            if (Insert)
            {
                UserService.User_Insert(obj);
            }
            else
            {
                UserService.User_Update(obj);
            }
            BindGrid();
            pnShow.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdUser_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdUser.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        private void ShowDll()
        {
            dllGroupUser_Id.Items.Add(new ListItem("===All===", ""));
            dllGroupUser_Id_Update.Items.Add(new ListItem("===Select Group User===", ""));
            List<GroupUser> dt = new List<GroupUser>();
            dt = GroupUserService.GroupUser_GetByAll();
            for (int i = 0; i < dt.Count; i++)
            {
                dllGroupUser_Id.Items.Add(new ListItem(dt[i].Name.ToString(), dt[i].Id.ToString()));
                dllGroupUser_Id_Update.Items.Add(new ListItem(dt[i].Name.ToString(), dt[i].Id.ToString()));
            }
            dllGroupUser_Id_Update.DataBind();

            ddlStatus.Items.Add(new ListItem("True", "True"));
            ddlStatus.Items.Add(new ListItem("False", "False"));
            ddlStatus.SelectedIndex = 0;
            ddlStatus.DataBind();
            
        }

        protected void btnshow_Click(object sender, EventArgs e)
        {
            if (dllGroupUser_Id.SelectedIndex > 0)
            {
                grdUser.DataSource = UserService.User_GetByGroupUser_Id(dllGroupUser_Id.SelectedValue);
                grdUser.DataBind();
            }
            else
            {
                grdUser.DataSource = UserService.User_GetByAll();
                grdUser.DataBind();
            }
        }

        protected void grdUser_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdUser.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut",
                                              "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick",
                                                             "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex +
                                                             ")");
                    }
                }
            }
        }
    }
}
